#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?EOF:*p1++)
template<typename TP> inline TP read(TP &num)
{
	TP x=0;
	int f=0;
	char ch=getchar();
	while(ch<48||ch>57) f|=ch=='-',ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return num=f?-x:x;
}
template<typename ...Args> inline void read(Args &...args)
{
	(read(args),...);
}
template<typename TP> inline void write(TP x)
{
	(x<0)?(putchar('-'),x=-x):0;
	(x>9)?(write(x/10),0):0;
	putchar((x%10)^48);
}
template<typename TP> inline void writeln(TP x)
{
	write<TP>(x);
	puts("");
}
constexpr int B=16,mod=999999999;
int border[251][251][2],Bsum[251][251],belong[4001][4001][2],val[4001][4001];
signed main()
{
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	int n,q; read(n,q);
	int Bcnt=n/B+!!(n%B);
	rep(i,1,Bcnt)
	{
		int lx=(i-1)*B+1,rx=min(n,lx+B-1);
		rep(j,1,Bcnt)
		{
			int ly=(j-1)*B+1,ry=min(n,ly+B-1);
			border[i][j][0]=rx,border[i][j][1]=ry;
			rep(L,lx,rx) rep(R,ly,ry) belong[L][R][0]=i,belong[L][R][1]=j;
		}
	}
	while(q--)
	{
		int opt; read(opt);
		if(opt==1)
		{
			int type,rc,calc,arg; read(type,rc,calc,arg);
			if(calc==4)
			{
				if(arg==mod) arg=0;
				if(arg==mod+1) arg=1;
			}
			auto getNew=[&](int orig)
			{
				return calc==1?(orig&arg):(
						calc==2?(orig|arg):(
							calc==3?(orig^arg):(
								(orig+arg)>=mod?(orig+arg)-mod:orig+arg
							)
						)
					);
			};
			int i=!type?rc:1,j=type?rc:1;
			for(;(type?i:j)<=n;++(type?i:j))
			{
				int diff;
				if(calc==4&&val[i][j]>=mod) diff=getNew(val[i][j]-mod)-val[i][j];
				else diff=getNew(val[i][j])-val[i][j];
				auto [Bl,Br]=belong[i][j];
				Bsum[Bl][Br]+=diff,val[i][j]+=diff;
			}
		}
		else
		{
			int xlim,ylim; read(xlim,ylim);
			int until_x=belong[xlim][1][0]-1,until_y=belong[1][ylim][1]-1,ans=0;
			if(border[until_x+1][1][0]==xlim) ++until_x;
			if(border[1][until_y+1][1]==ylim) ++until_y;
			rep(Bx,1,until_x) rep(By,1,until_y) ans+=Bsum[Bx][By];
			int start_x=border[until_x][1][0]+1,start_y=border[1][until_y][1]+1;
			int end_x=xlim,end_y=ylim;
			rep(i,start_x,end_x) rep(j,1,start_y-1) ans+=val[i][j];
			rep(i,1,start_x-1) rep(j,start_y,end_y) ans+=val[i][j];
			rep(i,start_x,end_x) rep(j,start_y,end_y) ans+=val[i][j];
			writeln(ans);
		}
	}
	cerr<<"Time elapsed: "<<clock()*1.0/1000000<<" seconds."<<endl;
	return 0;
}
